forEach 和 for迴圈


Posted by hoyi-23 on 2021-05-10

forEach和for迴圈

forEach 基本上可以達成for迴圈的所有需求
很大的差異在於 : for 可以被中斷 break; forEach不可被中斷!!!

//此為for loop
var array=['1','2','3'];
for(var i=0;i<array.length;i++){
console.log(i,array[i])
}
//此為forEach
array.forEach(function(array[i],i){
console.log(i,array[i])
}

forEach傳入的參數有三,大多只使用前兩者
1.單一物件 (單一陣列的值)
2.物件索引 (陣列的索引值)
3.完整的陣列本體 (陣列本體array)










Related Posts

給自己的~儲存方式有3種:cookie、local storage、session storage

給自己的~儲存方式有3種:cookie、local storage、session storage

React-[串接api篇]-登入與驗證功能發送post ajax call

React-[串接api篇]-登入與驗證功能發送post ajax call

用 Python 自學資料科學與機器學習入門實戰:Pandas 基礎入門

用 Python 自學資料科學與機器學習入門實戰:Pandas 基礎入門


Comments